﻿using System.ComponentModel.DataAnnotations;
using OpusSuite.Models.Model.Shared;
using OpusSuite.Utility;
using System;
using System.Collections.Generic;

namespace OpusSuite.Models.Model.WebAdmin
{
    [Table("user")]
    public class User : ModelBase
    {
        [Key]
        public int UserId { get; set; }

        [Required]
        [StringLength(256, MinimumLength=4)]
        public string UserName { get; set; }

        [Required]
        [StringLength(256, MinimumLength=4)]
        public string Password { get; set; }

        public int StatusValue { get; set; }
        public Enums.UserStatus Status
        {
            get { return (Enums.UserStatus)StatusValue; }
            set { StatusValue = (int)value; }
        }

        public virtual UserProfile Profile { get; set; }

        public virtual Person Person { get; set; }

        public virtual UserSession Session { get; set; }

        public virtual IList<UserSessionHistory> SessionHistory { get; set; }

        public void SetPassword(string newPassword)
        {
            Password = CryptoUtil.EncryptStringAES(newPassword, ConfigUtil.AESKey());
        }

        public bool ValidatePassword(string oldPassword)
        {
            string salt = CryptoUtil.GetSalt(Password);
            string sValidate = CryptoUtil.EncryptStringAES(oldPassword, ConfigUtil.AESKey(), salt);

            return (string.Compare(sValidate, Password, StringComparison.CurrentCulture) == 0);
        }
    }
}

